import type {
  BlogFrameworkConfig,
  PlatformElementConfig,
  StyleConfigMap,
} from '../../interfaces'
import { HEADING_TAG } from '../base'
import { preprocessElementConfigs } from '../personalization'
import { COMMON_PAGE_ELEMENTS } from './common'

/**
 * 页面元素配置
 */
export const CSDN_PAGE_ELEMENTS: Record<string, PlatformElementConfig> = {
  ...COMMON_PAGE_ELEMENTS,
  // 基础容器
  CONTENT_CONTAINER: {
    id: 'content-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: '.blog-content-box',
    inherit: false,
    styleVar: '--page-content-container',
  },
  META_CONTAINER: {
    id: 'meta-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 20,
    selector: '.article-header-box',
    inherit: false,
    styleVar: '--page-meta-container',
  },
  TITLE_CONTAINER: {
    id: 'title-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 20,
    selector: '.article-title-box',
    inherit: false,
    styleVar: '--page-title-container',
  },
  INFO_CONTAINER: {
    id: 'meta-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 30,
    selector: '.article-info-box',
    inherit: false,
    styleVar: '--page-info-container',
  },
  ARTICLE_CONTAINER: {
    id: 'article-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 55,
    selector: 'article',
    inherit: false,
    styleVar: '--page-article-container',
  },
  // 资源下载
  RESOURCE_DOWNLOAD: {
    id: 'resource-download',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 50,
    selector: '.article-resource-info-box',
    inherit: false,
    styleVar: '--page-resource-download',
  },
  // 活动挑战
  ACTIVITY_CHALLENGE: {
    id: 'activity-challenge',
    category: 'content-container',
    categoryKey: 'category',
    priority: 45,
    selector: '#creatActivityHref',
    inherit: false,
    styleVar: '--page-activity-challenge',
  },
  // 文章标题
  ARTICLE_TITLE: {
    id: 'article-title',
    category: 'title-container',
    categoryKey: 'category',
    priority: 40,
    selector: '.article-title-box .title-article',
    inherit: false,
    styleVar: '--page-article-title',
  },
  // 文章元数据
  ARTICLE_AUTHOR: {
    id: 'article-author',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 45,
    selector: '.article-info-box .follow-nickName',
    inherit: false,
    styleVar: '--page-article-author',
  },
  ARTICLE_COPYRIGHT_BUTTON: {
    id: 'article-copyright-button',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 45,
    selector: '.article-info-box .operating a, .article-info-box .href-article-edit-new .href-article-edit-click',
    inherit: false,
    styleVar: '--page-article-copyright-button',
  },
  ARTICLE_COPYRIGHT_WRAPPER: {
    id: 'article-copyright-wrapper',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 45,
    selector: '.article-info-box .slide-content-box',
    inherit: false,
    styleVar: '--page-article-copyright-wrapper',
  },
  ARTICLE_TAG: {
    id: 'article-tag',
    category: 'meta-container',
    categoryKey: 'category',
    priority: 50,
    selector: '.article-info-box .blog-tags-box .artic-tag-box a',
    inherit: false,
    styleVar: '--page-article-tag',
  },
  // 文章内容
  ARTICLE_CONTENT: {
    id: 'article-content',
    category: 'article-container',
    categoryKey: 'category',
    priority: 60,
    selector: 'article #article_content p',
    inherit: false,
    styleVar: '--page-article-content',
  },
  ARTICLE_HEADING: {
    id: 'article-heading',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: `article #article_content :is(${HEADING_TAG})`,
    inherit: false,
    styleVar: '--page-article-heading',
  },
  CONTENT_HR: {
    id: 'content-hr',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article_content hr',
    inherit: false,
    styleVar: '--page-content-hr',
  },
  CONTENT_PRE: {
    id: 'content-pre',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article_content pre',
    inherit: false,
    styleVar: '--page-content-pre',
  },
  CONTENT_CODE: {
    id: 'content-code',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article_content code',
    inherit: false,
    styleVar: '--page-content-code',
  },
  CONTENT_TABLE: {
    id: 'content-table',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article_content table',
    inherit: false,
    styleVar: '--page-content-table',
  },
  CONTENT_MARK: {
    id: 'content-mark',
    category: 'article-content',
    categoryKey: 'category',
    priority: 61,
    selector: 'article #article_content mark',
    inherit: false,
    styleVar: '--page-content-mark',
  },
  CONTENT_LINK: {
    id: 'content-link',
    category: 'article-content',
    categoryKey: 'category',
    priority: 62,
    selector: 'article #article_content a[href^="http"]',
    inherit: false,
    styleVar: '--page-content-link',
  },
  CONTENT_UL_LI: {
    id: 'content-ul-li',
    category: 'article-content',
    categoryKey: 'category',
    priority: 63,
    selector: 'article #article_content ul li',
    inherit: false,
    styleVar: '--page-content-li',
  },
  CONTENT_OL_LI: {
    id: 'content-ol-li',
    category: 'article-content',
    categoryKey: 'category',
    priority: 63,
    selector: 'article #article_content ol li',
    inherit: false,
    styleVar: '--page-content-ol-li',
  },
  CONTENT_BLOCK_QUOTE: {
    id: 'content-blockquote',
    category: 'article-content',
    categoryKey: 'category',
    priority: 64,
    selector: 'article #article_content blockquote',
    inherit: false,
    styleVar: '--page-content-blockquote',
  },
}
// 预处理配置
const PROCESSED_CSDN_ELEMENTS = preprocessElementConfigs(CSDN_PAGE_ELEMENTS)

export type CSDNElements = typeof CSDN_PAGE_ELEMENTS
export type CSDNElementKey = keyof typeof CSDN_PAGE_ELEMENTS
export type CSDNElementValue = typeof CSDN_PAGE_ELEMENTS[CSDNElementKey]

/**
 * 框架配置类型
 */
export type CSDNFrameworkConfig = {
  [K in keyof BlogFrameworkConfig]: Partial<StyleConfigMap>
}

/**
 * 框架配置
 */
export const CSDN_FRAMEWORK_CONFIG: CSDNFrameworkConfig = {
  GLOBAL: {
    shortcuts: [
      PROCESSED_CSDN_ELEMENTS.SHORTCUTS,
    ],
    pageTheme: [
      PROCESSED_CSDN_ELEMENTS.PAGE_THEME,
    ],
    loadingTheme: [
      PROCESSED_CSDN_ELEMENTS.PAGE_LOADING,
    ],
    loadingPhrases: [
      PROCESSED_CSDN_ELEMENTS.PAGE_LOADING,
    ],
    elementHide: [
      PROCESSED_CSDN_ELEMENTS.PAGE_ELEMENTS,
    ],
  },
  WRAPPER: {
  },
  TITLE: {
  },
  META: {
  },
  CONTENT: {
  },
}
